/**
 * 
 */
package jvs.vfs.tools;

import java.util.Properties;

import jvs.vfs.FileManager;
import x.javax.swing.filechooser.FileFilter;

/**
 * @author qiangli
 * 
 */
public class MimeTypeFileFilter extends FileFilter {
	public static final String AUDIO = "audio";
	public static final String IMAGE = "image";
	public static final String VIDEO = "video";
	public static final String TEXT = "text";
	public static final String APPLICATION = "application";

	protected Properties mimeTypes;

	protected String typeSlash;

	/**
	 * 
	 */
	public MimeTypeFileFilter(String type) {
		this.typeSlash = type + "/";
		loadMimeType();
	}

	private void loadMimeType() {
		mimeTypes = FileManager.getDefault().getContentTypes();
	}

	protected String getExt(String name) {
		int idx = name.lastIndexOf(".");
		if (idx > 0) {
			return name.substring(idx + 1).toLowerCase();
		}
		return null;
	}

	@Override
	public boolean accept(java.io.File file) {
		if (file.isDirectory()) {
			return true;
		}
		String ext = getExt(file.getName());
		if (ext == null) {
			return false;
		}
		String v = mimeTypes.getProperty(ext);
		if (v == null) {
			return false;
		}
		return (v.startsWith(typeSlash));
	}

	@Override
	public String getDescription() {
		return typeSlash+"*";
	}
	
}
